﻿//Manimaran

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Tzync
{
    public class DifferenceSubject : ISubject
    {
        bool changed = false;
        private ArrayList observers;
        private Difference difference;

        public DifferenceSubject()
        {
            observers = new ArrayList();
            difference = null;
        }

        public void RegisterObject(IObserver obj)
        {
            observers.Add(obj);
        }

        public void UnRegisterObject(IObserver obj)
        {
            int i = observers.IndexOf(obj);
            if (i >= 0 && i < observers.Count) observers.RemoveAt(i);
        }

        public void SetChanged()
        {
            changed = true;
        }

        public void NotifyObserver()
        {
            if (changed)
            {
                foreach (IObserver obj in observers)
                {
                    obj.Update(this);
                }
            }
        }

        public Difference GetDifference()
        {
            return difference;
        }

        public void SetDifference(Difference d)
        {
            difference = d;
            SetChanged();
            NotifyObserver();

        }
    }
}

